import { create, update, remove, query } from '../../../framework/service/index';

export default {

  namespace: 'allPartTime',

  state: {
    jobList: [],
    pageNum: 1,
    pageSize:10,
    pages: 0,
    ordeyIndex:0,
    areaIndex:0,
    typeNameIndex:0,
    SearchValue: '',
    isShow: -1,
    hiddenArr: [],
    selectIndex:1,
    ordey:[
      {name: '综合排序',id:1,value:''},
      {name: '最新发布',id:2,value:'publishtime'},
      {name: '热门兼职',id:3,value:'view'},
      {name: '附近兼职',id:4,value:'nearby'}
    ],
    key:'',
    typeId:'',
    district :'',
    screenTabIndex:0
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/all-part-time') {
          dispatch({type: 'query'});
          dispatch({type: 'relations/getTypes'});
          dispatch({type: 'relations/getAreas'});
          dispatch({
            type: 'querySuccess',
            payload:{
              isShow:-1,
              // ordeyIndex:0,
              // areaIndex:0,
              // typeNameIndex:0,
              // key:'',
              // typeId:'',
              // district :'',
            }
          })
        }
      })
    },
  },

  effects: {
    *query({ payload }, { call, put,select }) {

      let { pageSize,key,typeId,district } = yield select (({allPartTime}) => allPartTime);
      const { cityName } = yield select (({relations}) => relations);
      const city = cityName.slice(0,cityName.length-1);
      const pageNum = payload && payload.pageNum ? payload.pageNum : 1;

      if(typeId == '0'){
        typeId = '';
      }
      if(district == '区域'){
        district = '';
      }
      console.log('key', JSON.stringify(key));
      console.log('typeId', JSON.stringify(typeId));
      console.log('city', JSON.stringify(city));
      console.log('district', JSON.stringify(district));

      const { code, data, message } = yield call(query, '/api/pub/jobs', {
        pageNum,
        pageSize,
        key,
        typeId,
        district,
        city
      });

      // console.log('code', JSON.stringify(code));
      // console.log('data', JSON.stringify(data));
      // console.log('message', JSON.stringify(message));
      if (code === 200 && data) {

        const currentList = yield select(({ allPartTime }) => allPartTime.jobList);
        let list = data.records;
        if (pageNum > 1) {
          list = currentList.concat(data.records);
        }
        let newList = [];

        list.map((item, index) => {
          if(item.status == 'APPLYING'){
            newList.push(item);
          }
        })

        yield put({
          type: 'querySuccess',
          payload: {
            jobList: newList,
            pageNum: data.current,
            pageSize: data.size,
            pages: data.pages,
          }
        })
      }
    },

    //按排序获取兼职；
    *toOordeyJob({ payload }, { call, put,select }) {
      let pageSize = yield select (({allPartTime}) => allPartTime.pageSize);
      let pageNum = yield select (({allPartTime}) => allPartTime.pageNum);
// pageNum:1,pageSize:10
      console.log('分页');
      console.log(payload);
      if ( payload != undefined ) {
        if (payload.pageNum) {
          pageNum = payload.pageNum
          pageSize = payload.pageSize
        }
      }

      const { code, data, message } = yield call(query, '/api/jobs', {pageNum:1,pageSize:pageSize});
      console.log('data全部兼职',data);
      if (code === 200 && data) {
        yield put({
          type: 'querySuccess',
          payload: {
            jobList: data.records,
            pageNum: pageNum
          }
        })
      }
    },

    // *getTypes({ payload }, { call, put, select }) {
    //   const { code, data, message } = yield call(query, '/api/jobs/types/all', {});
    //   const typeArr = yield select(({allPartTime}) => allPartTime.typeArr);
    //
    //   console.log('code', JSON.stringify(code));
    //   if (code === 200 && data) {
    //     for (let i=0;i<data.length;i++) {
    //       typeArr.push(data[i])
    //     }
    //     yield put({
    //       type: 'querySuccess',
    //       payload: {
    //         typeArr: typeArr
    //       }
    //     })
    //   }
    // },
    *toTypeJob({ payload }, { call, put, select }) {
      console.log('toTypeJob',payload)

      let pageSize = yield select (({allPartTime}) => allPartTime.pageSize);
      let pageNum = yield select (({allPartTime}) => allPartTime.pageNum);
      const typeArr = yield select(({allPartTime}) => allPartTime.typeArr);

      let typeName = ''
      if ( payload != undefined ) {
        if (payload.typeNameIndex==0) {
          typeName =  ''
        } else {
          typeName = typeArr[payload.typeNameIndex].name
        }
        console.log(typeArr[payload.typeNameIndex].name);
        if (payload.pageNum) {
          pageNum = payload.pageNum
          pageSize = payload.pageSize
        }
      }
      const { code, data, message } = yield call(query, '/api/jobs', {typeName: typeName,pageNum:1,pageSize:pageSize});


      console.log('code', JSON.stringify(code));
      if (code === 200 && data) {
        for (let i=0;i<data.length;i++) {
          typeArr.push(data[i])
        }
        yield put({
          type: 'querySuccess',
          payload: {
            jobList: data.records

          }
        })
      }
    },
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
